statefiles: move dhcpv6_ia_enum_addrs() to odhcpd.c
authorDavid Härdeman <[email protected]>
Sat, 8 Nov 2025 13:40:39 +0000 (14:40 +0100)
committerÁlvaro Fernández Rojas <[email protected]>
Tue, 11 Nov 2025 07:30:23 +0000 (08:30 +0100)
dhcpv6_ia_enum_addrs() is used in several different places (ubus,
statesfile, dhcpv6-ia), so move it to a more central location. At the
same time, rename it to odhcpd_enum_addr6().

Signed-off-by: David Härdeman <[email protected]>
Link: https://github.com/openwrt/odhcpd/pull/302
Signed-off-by: Álvaro Fernández Rojas <[email protected]>
src/dhcpv6-ia.c
src/odhcpd.c
src/odhcpd.h
src/statefiles.c
src/statefiles.h
src/ubus.c

index 1729a054dd92f3606aca8fb10216c20c53f56154..f5a22d2f0a56dedb4793e542e9a6e6c4ca758a05 100644 (file)
@@ -884,7 +884,7 @@ static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
                                        .buf_len = sizeof(leasebuf),
                                        .buf_idx = 0 };
 
-               dhcpv6_ia_enum_addrs(iface, a, now, dhcpv6_log_ia_addr, &ctxt);
+               odhcpd_enum_addr6(iface, a, now, dhcpv6_log_ia_addr, &ctxt);
        }
 
        info("DHCPV6 %s %s from %s on %s: %s %s", type, (is_pd) ? "IA_PD" : "IA_NA",
index df57a49d63a4c8f1738754c01037d5bcad41aae9..49912eda4ac87f12cfc314fcff9cfe16a71996fb 100644 (file)
@@ -42,7 +42,9 @@
 #include <sys/random.h>
 
 #include <libubox/uloop.h>
+
 #include "odhcpd.h"
+#include "dhcpv6-ia.h"
 
 static int ioctl_sock = -1;
 
@@ -663,6 +665,63 @@ void odhcpd_bmemcpy(void *av, const void *bv, size_t bits)
        }
 }
 
+void odhcpd_enum_addr6(struct interface *iface, struct dhcpv6_lease *lease,
+                      time_t now, odhcpd_enum_addr6_cb_t func, void *arg)
+{
+       struct odhcpd_ipaddr *addrs = iface->addr6;
+       size_t m = get_preferred_addr(addrs, iface->addr6_len);
+
+       for (size_t i = 0; i < iface->addr6_len; ++i) {
+               struct in6_addr addr;
+               uint32_t preferred_lt, valid_lt;
+               int prefix = lease->length;
+
+               if (!valid_addr(&addrs[i], now))
+                       continue;
+
+               /* Filter Out Prefixes */
+               if (ADDR_MATCH_PIO_FILTER(&addrs[i], iface)) {
+                       char addrbuf[INET6_ADDRSTRLEN];
+                       info("Address %s filtered out on %s",
+                            inet_ntop(AF_INET6, &addrs[i].addr.in6, addrbuf, sizeof(addrbuf)),
+                            iface->name);
+                       continue;
+               }
+
+               if (lease->flags & OAF_DHCPV6_NA) {
+                       if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs))
+                               continue;
+
+                       addr = in6_from_prefix_and_iid(&addrs[i], lease->assigned_host_id);
+               } else {
+                       if (!valid_prefix_length(lease, addrs[i].prefix))
+                               continue;
+
+                       addr = addrs[i].addr.in6;
+                       addr.s6_addr32[1] |= htonl(lease->assigned_subnet_id);
+                       addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
+               }
+
+               preferred_lt = addrs[i].preferred_lt;
+               if (preferred_lt > (uint32_t)lease->preferred_until)
+                       preferred_lt = lease->preferred_until;
+
+               if (preferred_lt > (uint32_t)lease->valid_until)
+                       preferred_lt = lease->valid_until;
+
+               if (preferred_lt != UINT32_MAX)
+                       preferred_lt -= now;
+
+               valid_lt = addrs[i].valid_lt;
+               if (valid_lt > (uint32_t)lease->valid_until)
+                       valid_lt = lease->valid_until;
+
+               if (valid_lt != UINT32_MAX)
+                       valid_lt -= now;
+
+               func(lease, &addr, prefix, preferred_lt, valid_lt, arg);
+       }
+}
 
 int odhcpd_parse_addr6_prefix(const char *str, struct in6_addr *addr, uint8_t *prefix)
 {
index 613f8f517c59faeb6ec809bc58e82e09274191ec..5eaf4ce97fc45d01295513dba940674d2c2cc4e4 100644 (file)
@@ -534,11 +534,12 @@ const char *odhcpd_print_mac(const uint8_t *mac, const size_t len);
 int odhcpd_bmemcmp(const void *av, const void *bv, size_t bits);
 void odhcpd_bmemcpy(void *av, const void *bv, size_t bits);
 
-typedef void (*dhcpv6_binding_cb_handler_t)(struct dhcpv6_lease *lease,
-                                           struct in6_addr *addr, int prefix,
-                                           uint32_t pref, uint32_t valid,
-                                           void *arg);
-
+typedef void (*odhcpd_enum_addr6_cb_t)(struct dhcpv6_lease *lease,
+                                      struct in6_addr *addr, int prefix,
+                                      uint32_t pref, uint32_t valid,
+                                      void *arg);
+void odhcpd_enum_addr6(struct interface *iface, struct dhcpv6_lease *lease,
+                      time_t now, odhcpd_enum_addr6_cb_t func, void *arg);
 int odhcpd_parse_addr6_prefix(const char *str, struct in6_addr *addr, uint8_t *prefix);
 int odhcpd_netmask2bitlen(bool v6, void *mask);
 bool odhcpd_bitlen2netmask(bool v6, unsigned int bits, void *mask);
index d0adb09936c39577851095678904cfb3ee299104..c3476e41c95f3a40d4808237933ed3c5b1c123ce 100644 (file)
 
 static uint8_t statemd5[16];
 
-void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcpv6_lease *lease,
-                         time_t now, dhcpv6_binding_cb_handler_t func, void *arg)
-{
-       struct odhcpd_ipaddr *addrs = iface->addr6;
-       size_t m = get_preferred_addr(addrs, iface->addr6_len);
-
-       for (size_t i = 0; i < iface->addr6_len; ++i) {
-               struct in6_addr addr;
-               uint32_t preferred_lt, valid_lt;
-               int prefix = lease->length;
-
-               if (!valid_addr(&addrs[i], now))
-                       continue;
-
-               /* Filter Out Prefixes */
-               if (ADDR_MATCH_PIO_FILTER(&addrs[i], iface)) {
-                       char addrbuf[INET6_ADDRSTRLEN];
-                       info("Address %s filtered out on %s",
-                            inet_ntop(AF_INET6, &addrs[i].addr.in6, addrbuf, sizeof(addrbuf)),
-                            iface->name);
-                       continue;
-               }
-
-               if (lease->flags & OAF_DHCPV6_NA) {
-                       if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs))
-                               continue;
-
-                       addr = in6_from_prefix_and_iid(&addrs[i], lease->assigned_host_id);
-               } else {
-                       if (!valid_prefix_length(lease, addrs[i].prefix))
-                               continue;
-
-                       addr = addrs[i].addr.in6;
-                       addr.s6_addr32[1] |= htonl(lease->assigned_subnet_id);
-                       addr.s6_addr32[2] = addr.s6_addr32[3] = 0;
-               }
-
-               preferred_lt = addrs[i].preferred_lt;
-               if (preferred_lt > (uint32_t)lease->preferred_until)
-                       preferred_lt = lease->preferred_until;
-
-               if (preferred_lt > (uint32_t)lease->valid_until)
-                       preferred_lt = lease->valid_until;
-
-               if (preferred_lt != UINT32_MAX)
-                       preferred_lt -= now;
-
-               valid_lt = addrs[i].valid_lt;
-               if (valid_lt > (uint32_t)lease->valid_until)
-                       valid_lt = lease->valid_until;
-
-               if (valid_lt != UINT32_MAX)
-                       valid_lt -= now;
-
-               func(lease, &addr, prefix, preferred_lt, valid_lt, arg);
-       }
-}
-
 struct write_ctxt {
        FILE *fp;
        md5_ctx_t md5;
@@ -182,8 +124,8 @@ static void statefiles_write_hosts(time_t now)
                                        continue;
 
                                if (INFINITE_VALID(lease->valid_until) || lease->valid_until > now)
-                                       dhcpv6_ia_enum_addrs(ctxt.iface, lease, now,
-                                                            dhcpv6_write_ia_addrhosts, &ctxt);
+                                       odhcpd_enum_addr6(ctxt.iface, lease, now,
+                                                         dhcpv6_write_ia_addrhosts, &ctxt);
                        }
                }
 
@@ -251,7 +193,7 @@ static void statefiles_write_dhcpv6_lease(struct write_ctxt *ctxt, struct dhcpv6
                                          lease->assigned_subnet_id, lease->length);
 
        if (INFINITE_VALID(lease->valid_until) || lease->valid_until > ctxt->now)
-               dhcpv6_ia_enum_addrs(ctxt->iface, lease, ctxt->now, dhcpv6_write_ia_addr, ctxt);
+               odhcpd_enum_addr6(ctxt->iface, lease, ctxt->now, dhcpv6_write_ia_addr, ctxt);
 
        ctxt->buf[ctxt->buf_idx - 1] = '\n';
        fwrite(ctxt->buf, 1, ctxt->buf_idx, ctxt->fp);
index f6fc93393d8905082ea0005df33f51e3a5d86a65..9acd9ab4d918d430ea5944430c50ac43dc9bf323 100644 (file)
@@ -7,9 +7,6 @@
 #ifndef _STATEFILES_H_
 #define _STATEFILES_H_
 
-void dhcpv6_ia_enum_addrs(struct interface *iface, struct dhcpv6_lease *lease,
-                         time_t now, dhcpv6_binding_cb_handler_t func, void *arg);
-
 bool statefiles_write(void);
 
 #endif /* _STATEFILES_H_ */
index 137605e5347a36f9203283a279fd917db37d62cc..e589d4e00a7586cb32a5d26f90eb87a7f53467d8 100644 (file)
@@ -153,7 +153,7 @@ static int handle_dhcpv6_leases(_unused struct ubus_context *ctx, _unused struct
                        blobmsg_close_array(&b, m);
 
                        m = blobmsg_open_array(&b, a->flags & OAF_DHCPV6_NA ? "ipv6-addr": "ipv6-prefix");
-                       dhcpv6_ia_enum_addrs(iface, a, now, dhcpv6_blobmsg_ia_addr, NULL);
+                       odhcpd_enum_addr6(iface, a, now, dhcpv6_blobmsg_ia_addr, NULL);
                        blobmsg_close_array(&b, m);
 
                        blobmsg_add_u32(&b, "valid", INFINITE_VALID(a->valid_until) ?